<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Goods extends Controller
{

    public function index()
    {
        $goods=Db::name("goods g")->join("types t","g.typeId = t.tId")->select();
        $type = Db::name("types")->select();
        $this->assign("type",$type);
        $this->assign("goods",$goods);
        return $this->fetch();
    }

    public function update($tid,$gid)
    {
        // var_dump($content);
        $res = Db::name('goods')->where('gId', $gid)->update(['typeId' => $tid]);
        $type = Db::name('types')->where('tId',$tid)->find();
        // echo json_encode($res);
        if($res){
            // echo $type["tName"];
            echo json_encode(['tname'=>$type['tName'],'msg'=>'done']);
        }else{
            echo json_encode(['tname'=>$type['tName'],'msg'=>'err']);
        }
    }
    public function edit()
    {
        $type = Db::name('types')->select();
        // dump($net);
        echo json_encode($type);
    }
    public function del($id)
    {
        if (Db::name('goods')->delete($id)) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('删除失败', 'index');
        }
    }
}
